package edu.hrbu.trade01.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.hrbu.trade01.entity.Commence;
import edu.hrbu.trade01.mapper.CommenceMapper;
import edu.hrbu.trade01.service.CommenceService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;

@Service
public class CommenceServiceImpl extends ServiceImpl<CommenceMapper, Commence> implements CommenceService {
    // 条件分页查询
    @Override
    public Page<Commence> getPageWithCondition(int page, int limit, String commenceStr) {
        Page<Commence> pageParam = new Page<>(page, limit);
        LambdaQueryWrapper<Commence> wrapperCommence = new LambdaQueryWrapper<>();

        if (!ObjectUtils.isEmpty(commenceStr)) {
            Commence commence = JSONUtil.toBean(commenceStr, Commence.class);
            if (commence!=null) {
                String nameZh = commence.getNameZh();
                String firmName = commence.getFirmName();
                LocalDateTime createTime = commence.getCreateTime();
                String type = commence.getType();
                if (StrUtil.isNotBlank(nameZh)) wrapperCommence.like(Commence::getNameZh,nameZh);
                if (StrUtil.isNotBlank(firmName)) wrapperCommence.like(Commence::getFirmName, firmName);
                if (!ObjectUtils.isEmpty(createTime)) wrapperCommence.le(Commence::getCreateTime, createTime);
                if (StrUtil.isNotBlank(type)) wrapperCommence.eq(Commence::getType, type);

            }
        }
        return page(pageParam, wrapperCommence);
    }
}
